package com.lmy.demo0.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.lmy.demo0.common.Result;
import java.util.List;

import com.lmy.demo0.service.IBuyService;
import com.lmy.demo0.entity.Buy;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mingzi
 * @since 2022-08-15
 */
@RestController
@RequestMapping("/buy")
public class BuyController {

    @Resource
    private IBuyService buyService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Buy buy) {
        return Result.success(buyService.saveOrUpdate(buy));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(buyService.removeById(id));
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(buyService.removeByIds(ids));
    }

    @GetMapping
    public Result findAll() {
        return Result.success(buyService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(buyService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam String account) {
        QueryWrapper<Buy> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.like("account", account);

        return Result.success(buyService.page(new Page<>(pageNum, pageSize), queryWrapper));

    }
}


